/**
 * @author caojj
 * @version 	1.0.0.4
 * @usage		var win = new FWK.UI.Window("cc", "HOHOHO标题", true);
 *				document.getElementById("winCot").appendChild(win.getDom());
 *			
 *				setTimeout('win.setTitle("标题dangerous")',2000);
 */
try {
	if(!FWK.UI){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.UI = {}
}


FWK.UI.Window = function(id, title, closeAble) {
	this.id = id;
	this.title = title;
	this.closeAble = closeAble;
	
	this.dom = null;
	
	this.doms = {};
	
	this.init();
//	this.headDom = null;
//	this.bodyDom = null;
//	this.footDom = null;
}

FWK.UI.Window.prototype = {
	init : function() {
		var win = document.createElement("div");win.className = "opt";win.style.position="absolute";
		var optTL = document.createElement("div");optTL.className = "optTL";
			var optTR = document.createElement("div");optTR.className = "optTR";
				var optTM = document.createElement("div");optTM.className = "optTM";
		optTL.appendChild(optTR);optTR.appendChild(optTM);
				
		var optML = document.createElement("div");optML.className = "optML";
			var optMR = document.createElement("div");optMR.className = "optMR";
				var optMM = document.createElement("div");optMM.className = "optMM";
		optML.appendChild(optMR);optMR.appendChild(optMM);
				
		var optBL = document.createElement("div");optBL.className = "optBL";
			var optBR = document.createElement("div");optBR.className = "optBR";
				var optBM = document.createElement("div");optBM.className = "optBM";
		optBL.appendChild(optBR);optBR.appendChild(optBM);
		
		win.appendChild(optTL);win.appendChild(optML);win.appendChild(optBL);
		
		var titleDom = document.createElement("div");titleDom.className = "optTitle";
		
		var closeDom = document.createElement("a");closeDom.className = "optClose";closeDom.href = "javascript:;";closeDom.innerHTML="&nbsp;";
		
		// set property
		if (this.closeAble) {
			if (typeof(this.closeAble) == "function") {
				closeDom.onclick = this.closeAble;
			}
			closeDom.style.display = "";	
		} else {
			closeDom.style.display = "none";
		}
		
		
		optTM.appendChild(titleDom);
		optTM.appendChild(closeDom);
		
		// gather data reference
		this.doms = {
			dom : win,
			head: optTM,
			body: optMM,
			foot: optBM,
			
			title: titleDom,
			close: closeDom
		}

		if (this.title) {
			this.setTitle(this.title);
//			titleDom.innerHTML = this.title;
		}
		
		this.dom = win;
	},
	
	getDom : function(type) {
		var retDom = null;
		switch (type) {
			case "head":
			case "body":
			case "foot":				
			case "title":
				retDom = this.doms[type];
				break;
			default:
				retDom = this.dom;
				break;
		}
		return retDom;
	},
	
	setTitle : function(title, withTitleCot) {
		this.doms["title"].innerHTML = (withTitleCot && title!="") ? '<DIV class="optTitle"><DIV class="optTitleL"></DIV><DIV class="optTitleM">$1</DIV><DIV class="optTitleR"></DIV></DIV>'.replace("$1", title) : title;
	},
	setCloseFun : function(funref) {
		if (funref) {
			this.doms["close"].onclick = funref;
			this.doms["close"].style.display = "";	
		} else {
			this.doms["close"].onclick = null;
			this.doms["close"].style.display = "none";
		}		
	},
	dispose : function() {
		if (this.closeAble) {
			this.closeAble = null;
		}
		delete this.doms;
		delete this.dom;
	},
	
	/**
	 * 为win追加样式
	 * @param {Object} className
	 */
	attachClass : function(className) {
		this.doms["dom"].className = this.doms["dom"].className.split(" ")[0] + " " + className;
	}
}
